# 配置文件 - 配置中间层
# 此文件作为配置中间层，从config.ini读取配置并提供给其他模块使用
import os
import configparser

# 默认配置（当config.ini不存在或读取失败时使用）
DEFAULT_CONFIG = {
    'REQUEST_TIMEOUT': 10,
    'RETRY_COUNT': 3,
    'DELAY_TIME': 2,
    'DEBUG_MODE': False,
    'BASE_URL': 'https://finance.sina.com.cn',
    'MAIN_FORCE_URL': 'https://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/MoneyFlow.ssl_bkzj_ssggzj',
    'ENCODING': 'gb2312',
    'OUTPUT_FORMAT': 'excel',
    'OUTPUT_DIRECTORY': 'output',
    'MAIN_FORCE_FILENAME': 'main_force_data',
    'USER_AGENT': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'ACCEPT_LANGUAGE': 'zh-CN,zh;q=0.9,en;q=0.8',
    'CONNECTION': 'keep-alive',
    'REFERER': 'https://finance.sina.com.cn/'
}

# 初始化配置解析器和获取配置文件路径
config = configparser.ConfigParser()
current_dir = os.path.dirname(os.path.abspath(__file__))
config_file = os.path.join(current_dir, 'config.ini')

# 加载配置
if os.path.exists(config_file):
    try:
        config.read(config_file, encoding='utf-8')
        
        # 读取基础配置
        REQUEST_TIMEOUT = config.getint('基础配置', 'REQUEST_TIMEOUT', fallback=DEFAULT_CONFIG['REQUEST_TIMEOUT'])
        RETRY_COUNT = config.getint('基础配置', 'RETRY_COUNT', fallback=DEFAULT_CONFIG['RETRY_COUNT'])
        DELAY_TIME = config.getint('基础配置', 'DELAY_TIME', fallback=DEFAULT_CONFIG['DELAY_TIME'])
        DEBUG_MODE = config.getboolean('基础配置', 'DEBUG_MODE', fallback=DEFAULT_CONFIG['DEBUG_MODE'])
        
        # 读取网络配置
        BASE_URL = config.get('网络配置', 'BASE_URL', fallback=DEFAULT_CONFIG['BASE_URL'])
        MAIN_FORCE_URL = config.get('网络配置', 'MAIN_FORCE_URL', fallback=DEFAULT_CONFIG['MAIN_FORCE_URL'])
        ENCODING = config.get('网络配置', 'ENCODING', fallback=DEFAULT_CONFIG['ENCODING'])
        
        # 读取存储配置
        OUTPUT_FORMAT = config.get('存储配置', 'OUTPUT_FORMAT', fallback=DEFAULT_CONFIG['OUTPUT_FORMAT'])
        OUTPUT_DIRECTORY = config.get('存储配置', 'OUTPUT_DIRECTORY', fallback=DEFAULT_CONFIG['OUTPUT_DIRECTORY'])
        MAIN_FORCE_FILENAME = config.get('存储配置', 'MAIN_FORCE_FILENAME', fallback=DEFAULT_CONFIG['MAIN_FORCE_FILENAME'])
        
        # 读取HTTP头配置
        HEADERS = {
            'User-Agent': config.get('HTTP头', 'USER_AGENT', fallback=DEFAULT_CONFIG['USER_AGENT']),
            'Accept': config.get('HTTP头', 'ACCEPT', fallback=DEFAULT_CONFIG['ACCEPT']),
            'Accept-Language': config.get('HTTP头', 'ACCEPT_LANGUAGE', fallback=DEFAULT_CONFIG['ACCEPT_LANGUAGE']),
            'Connection': config.get('HTTP头', 'CONNECTION', fallback=DEFAULT_CONFIG['CONNECTION']),
            'Referer': config.get('HTTP头', 'REFERER', fallback=DEFAULT_CONFIG['REFERER']),
            'Accept-Encoding': 'gzip, deflate, br',
            'Upgrade-Insecure-Requests': '1',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'same-origin',
        }
        
    except Exception as e:
        # 配置读取失败时使用默认配置
        print(f"配置文件读取失败，使用默认配置: {e}")
        REQUEST_TIMEOUT = DEFAULT_CONFIG['REQUEST_TIMEOUT']
        RETRY_COUNT = DEFAULT_CONFIG['RETRY_COUNT']
        DELAY_TIME = DEFAULT_CONFIG['DELAY_TIME']
        DEBUG_MODE = DEFAULT_CONFIG['DEBUG_MODE']
        BASE_URL = DEFAULT_CONFIG['BASE_URL']
        MAIN_FORCE_URL = DEFAULT_CONFIG['MAIN_FORCE_URL']
        ENCODING = DEFAULT_CONFIG['ENCODING']
        OUTPUT_FORMAT = DEFAULT_CONFIG['OUTPUT_FORMAT']
        OUTPUT_DIRECTORY = DEFAULT_CONFIG['OUTPUT_DIRECTORY']
        MAIN_FORCE_FILENAME = DEFAULT_CONFIG['MAIN_FORCE_FILENAME']
        HEADERS = {
            'User-Agent': DEFAULT_CONFIG['USER_AGENT'],
            'Accept': DEFAULT_CONFIG['ACCEPT'],
            'Accept-Language': DEFAULT_CONFIG['ACCEPT_LANGUAGE'],
            'Connection': DEFAULT_CONFIG['CONNECTION'],
            'Referer': DEFAULT_CONFIG['REFERER'],
            'Accept-Encoding': 'gzip, deflate, br',
            'Upgrade-Insecure-Requests': '1',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'same-origin',
        }
else:
    # 配置文件不存在时使用默认配置
    REQUEST_TIMEOUT = DEFAULT_CONFIG['REQUEST_TIMEOUT']
    RETRY_COUNT = DEFAULT_CONFIG['RETRY_COUNT']
    DELAY_TIME = DEFAULT_CONFIG['DELAY_TIME']
    DEBUG_MODE = DEFAULT_CONFIG['DEBUG_MODE']
    BASE_URL = DEFAULT_CONFIG['BASE_URL']
    MAIN_FORCE_URL = DEFAULT_CONFIG['MAIN_FORCE_URL']
    ENCODING = DEFAULT_CONFIG['ENCODING']
    OUTPUT_FORMAT = DEFAULT_CONFIG['OUTPUT_FORMAT']
    OUTPUT_DIRECTORY = DEFAULT_CONFIG['OUTPUT_DIRECTORY']
    MAIN_FORCE_FILENAME = DEFAULT_CONFIG['MAIN_FORCE_FILENAME']
    HEADERS = {
        'User-Agent': DEFAULT_CONFIG['USER_AGENT'],
        'Accept': DEFAULT_CONFIG['ACCEPT'],
        'Accept-Language': DEFAULT_CONFIG['ACCEPT_LANGUAGE'],
        'Connection': DEFAULT_CONFIG['CONNECTION'],
        'Referer': DEFAULT_CONFIG['REFERER'],
        'Accept-Encoding': 'gzip, deflate, br',
        'Upgrade-Insecure-Requests': '1',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
    }

# 构建主力排名配置字典
MAIN_FORCE_CONFIG = {
    'base_url': BASE_URL,
    'main_force_url': MAIN_FORCE_URL,
    'encoding': ENCODING,
    'target_table_keywords': ['主力', '排名', '资金', '净流入', '净流出', '主力资金'],
    'expected_columns': ['排名', '股票代码', '股票名称', '主力净流入', '涨跌幅', '换手率', '成交额']
}